package schedulo.gui;

import java.util.AbstractSequentialList;
import java.util.LinkedList;
import java.util.List;

import javax.swing.AbstractListModel;

import schedulo.core.task.Task;
import schedulo.core.task.provider.TaskProvider;
import schedulo.core.task.provider.TaskProviderFactory;
import schedulo.core.task.provider.TaskProviderFactoryListener;

public class TaskProviderListModel extends AbstractListModel implements TaskProviderFactoryListener {

	private List<TaskProvider> taskProviders;
	
	public TaskProviderListModel() {
		taskProviders = new LinkedList<TaskProvider>();
		
		// register as task provider factory listener
		TaskProviderFactory.getInstance().addFactoryListener(this);
	}

	public Object getElementAt(int index) {
		return taskProviders.get(index);
	}

	public int getSize() {
		return taskProviders.size();
	}

	public List<TaskProvider> getTaskProviders() {
		return taskProviders;
	}

	public void taskProviderAdded(TaskProvider taskProvider) {
		taskProviders.add(taskProvider);
		fireIntervalAdded(this, taskProviders.indexOf(taskProvider), taskProviders.indexOf(taskProvider));
	}

	public void taskProviderRemoved(TaskProvider taskProvider) {
		fireIntervalRemoved(this, taskProviders.indexOf(taskProvider), taskProviders.indexOf(taskProvider));		
		taskProviders.remove(taskProvider);
	}
}
